package com.cskaoyan.controller.stage;

import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.UserService;
import com.cskaoyan.shiro.MallToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Random;

/**
 * @program: mall38
 * @description: 微信认证
 * @author: Lexis
 * @create: 2022-05-08 21:08
 **/
@RestController
@RequestMapping("wx/auth")
public class WxAuthController {
    @Autowired
    UserService userService;


    @PostMapping("login")
    public ExceptionVO login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        MallToken wxToken = new MallToken(username, password, "wx");
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(wxToken);
        } catch (AuthenticationException e) {
            return ExceptionVO.error(700, "账号或者密码不正确");
        }
        WxLoginDataVO loginUserDataVO = userService.getAdminInfoByName(username);
        loginUserDataVO.setToken((String) subject.getSession().getId());
        return BaseRespVO.ok(loginUserDataVO);
    }

    @PostMapping("logout")
    public ExceptionVO logout(HttpSession session) {
        session.invalidate();
        return ExceptionVO.fine();
    }

    /**
     * @program: mall38
     * @description: 发送验证码
     * @author: wlyq
     * @create: 2022-05-10 17:37
     */
    @PostMapping("regCaptcha")
    public ExceptionVO regCaptcha(@RequestBody Map Map) {
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            Integer num = random.nextInt(10);
            String s = num.toString();
            code += s;
        }
        userService.sendMsg((String) Map.get("mobile"), code);
        return ExceptionVO.fine();
    }

    @PostMapping("register")
    public ExceptionVO register(@RequestBody WxRegisterVO wxRegisterVO) {
        if (!userService.checkCode(wxRegisterVO.getMobile(), wxRegisterVO.getCode())) {
            return ExceptionVO.error(703, "验证码错误");
        }
        ExceptionVO exceptionVO = userService.userRegister(wxRegisterVO);
        if (exceptionVO.getErrno() != 0) {
            return exceptionVO;
        }
        MallToken wxToken = new MallToken(wxRegisterVO.getUsername(), wxRegisterVO.getPassword(), "wx");
        Subject subject = SecurityUtils.getSubject();
        subject.login(wxToken);
        WxLoginDataVO<UserLoginVO> wxLoginDataVO = new WxLoginDataVO<>();
        wxLoginDataVO.setToken((String) subject.getSession().getId());
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setNickName(wxRegisterVO.getUsername());
        userLoginVO.setAvatarUrl("http://localhost:8083/image/14651728-4fc5-4751-9854-b25b5b40b4a7-Yasuo0.jpg");
        wxLoginDataVO.setUserInfo(userLoginVO);
        return BaseRespVO.ok(wxLoginDataVO);
    }

    @PostMapping("reset")
    public ExceptionVO reset(@RequestBody Map map) {

        String phone = (String) map.get("mobile");
        String code = (String) map.get("code");
        String password = (String) map.get("password");
        return userService.reset(phone, password, code);
    }
}
